package Api;

import Util.HttpUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static Util.Tools.getMD5;

/**
 关闭支付订单
 返回示例: <?xml version="1.0" encoding="utf-8"?><root><ret_code>0000</ret_code><ret_msg>关闭成功</ret_msg><agent_id>1664502</agent_id><sign>6112b599134448783ea8799ee929e23e</sign></root>
 */

public class PaymentClose {

    public static void main(String[] args) {
        try {

            // 目标URL
            String url = "https://pay.heepay.com/API/Payment/PaymentClose.aspx";
            //需要的MD5密钥，商户后台退款版本一的密钥
            String key = "1C3A345D4F1E48B7A02B90AC";
            //商户编号，（汇付宝商户内码：七位整数数字）
            String agent_id = "1664502";
            //固定版本号
            String version ="1";
            //商户系统内部的支付订单号（要保证唯一）
            String agent_bill_id = "20240219143127";



            //拼接待加密参数
            StringBuilder builder = new StringBuilder();
            builder.append("agent_bill_id=");
            builder.append(agent_bill_id);
            builder.append("&agent_id=");
            builder.append(agent_id);
            builder.append("&key=");
            builder.append(key);
            builder.append("&version=");
            builder.append(version);

            String Datestr = builder.toString();
            String Date = Datestr.toLowerCase();
            System.out.println("待签名串："+Date);

            //MD5加密得出签名结果
            String sign = getMD5(Date);


            // 请求参数，以Map形式表示
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("version", version);
            parameters.put("agent_id", agent_id);
            parameters.put("agent_bill_id", agent_bill_id);
            parameters.put("sign", sign);

            String response = HttpUtil.sendPost2(url, parameters);
            System.out.println(response);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

